import type { App, Plugin } from 'vue'

// 全局组件安装
export const withInstall = <T>(component: T, alias?: string) => {
	const comp = component as any
	comp.install = (app: App) => {
		app.component(comp.name || comp.displayName, component)
		if (alias) {
			app.config.globalProperties[alias] = component
		}
	}
	return component as T & Plugin
}

// 时间格式化（yyyy-mm-dd)
export const formatDate = (date: any, format = 'yyyy-MM-dd') => {
	const o = {
		'y+': date.getFullYear(),
		'M+': date.getMonth() + 1,
		'd+': date.getDate(),
		'h+': date.getHours(),
		'm+': date.getMinutes(),
		's+': date.getSeconds()
	}
	for (let k in o) {
		if (new RegExp(`(${k})`).test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : `0${o[k]}`.substr(-RegExp.$1.length))
		}
	}
	return format
}
